
var DownLoad = new Class.create();
var downloadManager;
DownLoad.prototype = {

    initialize:function(){
        downloadManager = api.require('downloadManager')
    }
    ,add:function (url,object) {
        var _this = this
        downloadManager.enqueue({
            url: url,
            cache: true,
            allowResume: true,
            title: "",
            networkTypes: 'all'
        }, function(ret, err) {
            logJson(ret)
            logJson(err)
            if (ret) {
                showToast("加入下载列表")
                var id = ret.id
                _this._addLocalData(id,object)
                // console.log(JSON.stringify(ret));
            } else {
                console.log(JSON.stringify(err));
                showToast(JSON.stringify(err))
            }
        });
    }
    // 本地存储下载数据
    ,_addLocalData:function (id,data) {
        var oldData = getLocData("MyDownLoadData");

        var newData = {};
        newData["index_" + id] = {id:id,data:data,info:[]};
        if (isTrue(oldData)) {
            for (var i in oldData) {newData[i] = oldData[i]}
        }
        setLocData("MyDownLoadData",newData)
        return newData
    }

    ,_query:function (cbFunc) {
        downloadManager.query({}, function(ret, err) {
            if (ret) {
                cbFunc(ret.data)
            } else {
                return false;
            }
        });
    }

    ,getData:function (cbFunc) {

        var oldData = getLocData("MyDownLoadData");
        var newData = {};
        this._query(function (data) {
            if (!isTrue(data)){
                cbFunc([])
                return false;
            }
            data.map(function (d) {
                for (var i in oldData) {
                    if (d.id == oldData[i].id) {

                        var obj = {};
                        obj.id = oldData[i].id
                        obj.data = oldData[i].data
                        obj.info = d
                        var oldStatic
                        var time = getTimestamp();
                        if (oldData[i].static == undefined) {
                            oldStatic = {
                                last:time
                                ,total:(d.totalSize /1024 /1024).toFixed(2)
                                ,finish:0
                                ,status:d.totalSize == d.finishSize?1:0
                                ,speed:0
                            }
                        }else {
                            oldStatic = oldData[i].static
                        }
                        //计算下载速度
                        var finished = (d.finishSize /1024 /1024).toFixed(2)
                        log(time - oldStatic.last,"A=")
                        log(finished - oldStatic.finish,"B=")
                        log(time,"C=")
                        var speed = ((finished - oldStatic.finish) / (time - oldStatic.last)).toFixed(2)

                        obj.static = {
                            last:time
                            ,total:(d.totalSize /1024 /1024).toFixed(2)
                            ,finish:finished
                            ,status:d.totalSize == d.finishSize?1:0
                            ,speed:speed
                        };
                        newData[i] = obj
                    }
                }
            })

            setLocData("MyDownLoadData",newData)
            cbFunc(newData)
        });
    }



    ,pause:function (id) {
        downloadManager.pause({
            id: id
        }, function(ret, err) {
            if (ret.status) {
                // alert(JSON.stringify(ret));
            } else {
                // alert(JSON.stringify(err));
            }
        });
    }
    ,resume:function (id) {
        downloadManager.resume({
            id: id
        }, function(ret, err) {
            if (ret.status) {
                // alert(JSON.stringify(ret));
            } else {
                // alert(JSON.stringify(err));
            }
        });
    }
    ,openView:function () {
        downloadManager.openManagerView({
            title: '下载管理'
        }, function(ret, err) {
            if (ret) {
                log(JSON.stringify(ret));
            } else {
                log(JSON.stringify(err));
            }
        });
    }
    ,isDown: function (find) {
        var oldData = getLocData("MyDownLoadData");
        for (var i in oldData) {
            if (oldData[i].data.id == find) {return oldData[i].id}
            if (oldData[i].data.title == find) {return oldData[i].id}
            if (oldData[i].info.title == find) {return oldData[i].id}
        }
        return false;
    }
    ,getList:function () {
        var oldData = getLocData("MyDownLoadData");
        var data = [];
        for (var i in oldData) {
            data.push(oldData[i])
        }
        return data;
    }
    , del: function (id) {




        downloadManager.remove({
            ids: [id]
            ,deleteFiles:true
        }, function(ret, err) {
            if (ret) {
                log(JSON.stringify(ret));
            } else {
                log(JSON.stringify(err));
            }
        });
        var oldData = getLocData("MyDownLoadData");
        var data = [];
        for (var i in oldData) {
            data.push(oldData[i])
        }
        var newData = objDelIndex(oldData,"index_" + id)
        setLocData("MyDownLoadData",newData);
    }
}